<?php

class AdminUsersModel extends \think\Model
{
    protected $table = 'admin_user';
    protected $pk = 'id';

    const user_type_manager = 0;
    const user_type_promoter = 1;

    const status_allow_login = 1;
    const status_lock_login = 0;

    public function group()
    {
        return $this->belongsTo(AdminGroupsModel::class, 'groupid');
    }

    /**
     * 登录
     */
    static function saveLoginStatus($user)
    {
        $return = array(
            'status' => false,
            'message' => '未知错误'
        );
        // 分组权限
        $group = AdminGroupsModel::find($user->groupid);
        if ($group) {
            // 权限资源
            $rights = AdminRightsModel::where('id', 'in', explode(',', $group->rightlist))->select();
            $userRights = '';
            foreach ($rights as $item) {
                $userRights .= $item->content . ',';
            }
            $saveUser = array(
                'userinfo' => $user,
                'rights' => strtolower(trim($userRights, ',')),
            );
            // 保存用户状态
            $redis = Sredis::getInterface()->handle;
            $redis->hset('admin_user_token_cache', $user->id, json_encode([
                'last_login_ip' => Helper::ip(),
                'last_login_time' => time(),
                'last_login_sessionid' => session_id()
            ]));
            Yaf_Session::getInstance()->set(AdminController::admin_auth_session_key, serialize($saveUser));
            $return['status'] = true;
        } else {
            $return['message'] = '用户权限配置错误!';
        }
        return $return;
    }

    // 检测唯一登录
    static function checkOnlineUser($userid)
    {
        $redis = Sredis::getInterface()->handle;
        $cache_data = json_decode($redis->hget('admin_user_token_cache', $userid), true);
        if ($cache_data) {
            if ($cache_data['last_login_ip'] == Helper::ip()) {
                if (time() - $cache_data['last_login_time'] <= 24 * 60 * 60) {
                    // 12小时强制退出
                    if ($cache_data['last_login_sessionid'] == session_id()) return true;
                }
            }
        }
        Yaf_Session::getInstance()->set(AdminController::admin_auth_session_key, '');
        return false;
    }

    /**
     * 检测权限
     * @param unknown $controller
     * @param unknown $action
     */
    static function check_right($uri)
    {
        // 去除?后面
        if (strpos($uri, '?')) {
            $uri = explode('?', $uri);
            $uri = $uri[0];
        }
        $uri = trim(strtolower($uri), '/');
        $uri = str_replace('/', '@', $uri);
        if (substr_count($uri, '@') == 1) $uri = "admin@{$uri}";
        $user = Yaf_Session::getInstance()->get(AdminController::admin_auth_session_key);
        if (!$user) return false;
        $user = unserialize($user);
        $userRight = ",,admin@index@index,{$user['rights']},";
        return boolval(strpos($userRight, ",{$uri},"));
    }

    static function hash_password($password, $salt)
    {
        return md5(md5($password) . $salt);
    }
}